/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#define INTERFACEMODULE
#include "InterfaceContainer.h"

namespace ModularEngine
{



	InterfaceContainer::InterfaceContainer() : InterfaceObject()
	{
		return;
	}	
	
	void InterfaceContainer::Draw()
	{
		InterfaceObject::Draw();

	}

	void InterfaceContainer::LoadSchema( const char *rpXMLSchema )
	{

		doc = xmlParseFile( rpXMLSchema );
		if( !doc )
		{
			cout << "Document Was Not Successfully Parsed!" << endl;
			return;
		}

		cur = xmlDocGetRootElement( doc );

		if( !cur )
		{
			cout << "Document Is Empty" << endl;
			return;
		}

		if( !xmlStrcmp( cur->name, (const xmlChar *) "schema" ) )
		{
			xmlChar *name, *texture, *width, *height;
			name	= xmlGetProp( cur, (const xmlChar *)"name" );
			texture = xmlGetProp( cur, (const xmlChar *)"texture" );
			width	= xmlGetProp( cur, (const xmlChar *)"width" );
			height	= xmlGetProp( cur, (const xmlChar *)"height" );

			mSchemaInfo.mpName = new char[xmlStrlen( name )+1];
			mSchemaInfo.mpTexture = new char[xmlStrlen( texture )+1];

			sprintf( mSchemaInfo.mpName, "%s", name );
			sprintf( mSchemaInfo.mpTexture, "%s", texture );
			mSchemaInfo.mHeight = xmlToInt( height );
			mSchemaInfo.mWidth = xmlToInt( width );

			xmlNodePtr element = cur->children;
			while( element )
			{
				if( !xmlStrcmp( element->name, (const xmlChar *)"element" ) )
					ParseElement( element );
				element = element->next;
			}

			xmlFree( name );
			xmlFree( texture );
			xmlFree( width );
			xmlFree( height );
		} else 
			cout << "Root Element Invalid!" << endl;

		xmlFreeDoc( doc );
	}

	void InterfaceContainer::ParseElement( xmlNodePtr rElement )
	{
		xmlChar *type = xmlGetProp( rElement, (const xmlChar *)"type" );
		if( !type )
		{
			cout << rElement->name << endl;
			cout << "No Element Type Given" << endl;
			return;
		}

		// Discover where we should put data here based on element type
		// int offset = GetOffset( 

		xmlNodePtr rItem = rElement->children;
		while( rItem )
		{

			// get all the data here.

			rItem = rItem->next;
		}

		xmlFree( type );

	}

	float InterfaceContainer::xmlToFloat(xmlChar *rXML)
	{
		float rValue;
		char *tXML = new char[xmlStrlen(rXML)+1];
		sprintf( tXML, "%s", rXML );
		rValue = atof( tXML );
		delete [] tXML;
		return rValue;		
	}

	int InterfaceContainer::xmlToInt(xmlChar *rXML)
	{
		int rValue;
		char *tXML = new char[xmlStrlen(rXML)+1];
		sprintf( tXML, "%s", rXML );
		rValue = atoi( tXML );
		delete [] tXML;
		return rValue;

	}

	char *InterfaceContainer::xmlToChar( xmlChar *rXML )
	{
		char *rValue = new char[xmlStrlen(rXML)+1];
		sprintf( rValue, "%s", rXML );
		return rValue;
	}

}